package com.xzh.homeliving.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzh.homeliving.commodity.entity.BrandEntity;
import org.springframework.web.bind.annotation.*;

import com.xzh.homeliving.commodity.entity.CategoryBrandRelationEntity;
import com.xzh.homeliving.commodity.service.CategoryBrandRelationService;
import com.xzh.common.utils.PageUtils;
import com.xzh.common.utils.R;

import javax.annotation.Resource;


/**
 * 品牌分类关联表
 *
 * @author xzh
 * @email ranpis02@qq.com
 * @date 2024-01-30 13:14:49
 */
@RestController
@RequestMapping("commodity/categorybrandrelation")
public class CategoryBrandRelationController {
    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 通过brandId和categoryId保存所有信息
     */
    @RequestMapping("/saveall")
    public R saveAll(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        categoryBrandRelationService.saveAll(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 直接返回所有的数据，而不以分页的形式返回
     */
    @RequestMapping("/listall")
    public R listAll(@RequestParam("brandId") Long brandId){
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
        return R.ok().put("data", data);
    }

    /**
     * 根据categoryId来获取其关联的所有品牌实体
     */
    @RequestMapping("/brands/list")
    public R getBrandByCategoryId(@RequestParam("catId") Long catId) {
        List<BrandEntity> brandEntities = categoryBrandRelationService.getBrandByCategoryId(catId);
        return R.ok().put("data", brandEntities);
    }
}
